package com.zrar.easyweb.demo.portal.service;

import com.zrar.easyweb.demo.portal.feign.UserFeign;
import com.zrar.easyweb.demo.portal.vo.UserDetails;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 用户服务
 *
 * @author green
 * @date 2019/11/14
 * @company 安人股份
 * @desc
 */
@Service
public class UserService {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private UserFeign userFeign;

    /**
     * 通过robbin方式调用远程接口获取用户信息
     *
     * @param userId
     * @return
     */
    public UserDetails getUserDetailsByRobbin(String userId) {
        // 此处采用了服务名称作为地址，RestTemplate实例必须配置@LoadBalanced负载均衡注解
        String url = "http://user-service/user/detail/" + userId;
        ResponseEntity<UserDetails> responseEntity = restTemplate.getForEntity(url, UserDetails.class);
        return responseEntity.getBody();
    }

    /**
     * 通过Feign方式调用远程接口获取用户信息
     *
     * @param userId
     * @return
     */
    public UserDetails getUserDetailsByFeign(String userId) {
        UserDetails detail = userFeign.detail(userId);
        return detail;
    }


}
